package com.passengerflowmanagementsystem.web.controller.em;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.passengerflowmanagementsystem.common.utils.poi.ExcelUtil;
import com.passengerflowmanagementsystem.statistics.domain.EmEquipment;
import com.passengerflowmanagementsystem.statistics.service.IEmEquipmentService;
import com.passengerflowmanagementsystem.statistics.utils.ModbusMasterUtils;
import com.intelligt.modbus.jlibmodbus.master.ModbusMaster;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.passengerflowmanagementsystem.common.annotation.Log;
import com.passengerflowmanagementsystem.common.core.controller.BaseController;
import com.passengerflowmanagementsystem.common.core.domain.AjaxResult;
import com.passengerflowmanagementsystem.common.enums.BusinessType;
import com.passengerflowmanagementsystem.common.core.page.TableDataInfo;

/**
 * 设备Controller
 * 
 * @author sheng
 * @date 2024-10-28
 */
@RestController
@RequestMapping("/em/equipment")
public class EmEquipmentController extends BaseController
{
    @Autowired
    private IEmEquipmentService emEquipmentService;

    /**
     * 查询设备列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EmEquipment emEquipment)
    {
        startPage();
        List<EmEquipment> list = emEquipmentService.selectEmEquipmentList(emEquipment);
        return getDataTable(list);
    }

    /**
     * 导出设备列表
     */
    @Log(title = "【导出设备列表】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmEquipment emEquipment)
    {
        List<EmEquipment> list = emEquipmentService.selectEmEquipmentList(emEquipment);
        ExcelUtil<EmEquipment> util = new ExcelUtil<EmEquipment>(EmEquipment.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取设备详细信息
     */
    @GetMapping(value = "/{equipmentId}")
    public AjaxResult getInfo(@PathVariable("equipmentId") Long equipmentId)
    {
        return success(emEquipmentService.selectEmEquipmentByEquipmentId(equipmentId));
    }

    /**
     * 新增设备
     */
    @Log(title = "【新增设备】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmEquipment emEquipment)
    {
        return toAjax(emEquipmentService.insertEmEquipment(emEquipment));
    }

    /**
     * 修改设备
     */
    @Log(title = "【修改设备】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmEquipment emEquipment)
    {
        return toAjax(emEquipmentService.updateEmEquipment(emEquipment));
    }

    /**
     * 删除设备
     */
    @Log(title = "【删除设备】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{equipmentIds}")
    public AjaxResult remove(@PathVariable Long[] equipmentIds)
    {
        return toAjax(emEquipmentService.deleteEmEquipmentByEquipmentIds(equipmentIds));
    }

    @GetMapping(value = "/getData")
    public AjaxResult getData(){
        ModbusMaster master = ModbusMasterUtils.getModbusMaster("192.168.1.30",8080);
        List<Float> floatList = ModbusMasterUtils.readInputRegisters(master,0001,101,400);
        List<Float> floatList1 = ModbusMasterUtils.readHoldingRegisters(master,0001,101,400);
        return toAjax(1);
    }
}
